let audio = new Audio();
// import $request from "../util/request";
// 引入 $store, 用于后续操作 vuex 
import $store from "../store/index";
console.log("vuex实例", $store);

audio.playSong = async function (id) {
    $store.dispatch("get_song_detail", id);
    audio.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`;
    audio.oncanplay = function () {
        audio.play();
    }
};
// 将音频播放状态 和 相关信息存至 vuex 中, 方便多页面使用
audio.addEventListener("playing", () => {
    console.log("播放中")
    $store.commit("change_state", true)
})
audio.addEventListener("pause", () => {
    console.log("暂停")
    $store.commit("change_state", false)
})
audio.addEventListener("timeupdate", () => {
    let obj = {
        current_time: parseInt(audio.currentTime),
        duration: parseInt(audio.duration)
    }
    // console.log(obj)
    $store.commit("change_current_song", obj)
})
export default audio;